//
//  SecondViewControllerOfFamous.m
//  FloatClouds
//
//  Created by 黄威 on 16/8/30.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "SecondViewControllerOfFamous.h"
#import "ViewPointView.h"
#import "HWLNetworkTool.h"
#import "Appreciate.h"
#import "AppreciateModel.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "ViewPointTableviewController.h"

@interface SecondViewControllerOfFamous ()
@property (nonatomic, retain) AppreciateModel *appreciate;
@property (nonatomic, assign) NSInteger sn;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, assign) NSInteger flag;
@property (nonatomic, strong) NSMutableArray *arrayOfRefreshData;
@end

@implementation SecondViewControllerOfFamous

- (void)viewDidLoad {
    [super viewDidLoad];
    self.flag = 0;
    self.arrayOfRefreshData = [NSMutableArray array];
    
    [self.tableView registerClass:[ViewPointView class] forCellReuseIdentifier:@"AppreciateCell"];
    [self handleData];
    self.sn = 0;
    __weak typeof(self) weakSelf = self;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf handleData];
        [self.activityIndicatorView startAnimating];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
              [weakSelf getMore];
        
    }];

}
- (void)viewWillAppear:(BOOL)animated{
[self createactivityIndicatorView];

}

- (void)createactivityIndicatorView{
    
    self.activityIndicatorView=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    self.activityIndicatorView.center=self.view.center;
    [self.activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.activityIndicatorView setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
}

- (void)handleData{
    self.flag = 0;
    [self.activityIndicatorView startAnimating];
    self.sn = 0;
    NSDictionary *dicOfBody = @{@"openudid":@"OPENUDID"};
    NSDictionary *dicOfHeader = @{@"Content-Type":@"application/x-www-form-urlencoded"};
    [HWLNetworkTool POST:@"http://app.yanyue.cn/index/appreciate" andBody:dicOfBody andBodyStyle:HWLBodyJSON andHeader:dicOfHeader andResponse:HWLJSON andSuccessBlock:^(id result) {
        
        
       
        self.appreciate = [AppreciateModel modelWithDic:result];
       [self.tableView reloadData];
       [self.tableView.mj_header endRefreshing];
       [self.activityIndicatorView stopAnimating];
        
    } andFailureBlock:^(NSError *error) {}];

}

- (void)getMore{

    self.flag = 1;
    
    
    
    [self.activityIndicatorView startAnimating];
     self.sn = self.sn + 10;
    NSString *sn = [NSString stringWithFormat:@"%ld",self.sn];
    NSString *dicOfBody = [@"sn="stringByAppendingString:sn];
    NSDictionary *dicOfHeader = @{@"Content-Type":@"application/x-www-form-urlencoded"};
    [HWLNetworkTool POST:@"http://app.yanyue.cn/index/appreciate" andBody:dicOfBody andBodyStyle:HWLBodyString andHeader:dicOfHeader andResponse:HWLJSON andSuccessBlock:^(id result) {
        
        self.arrayOfRefreshData = self.appreciate.appreciate.mutableCopy;
        self.appreciate = [AppreciateModel modelWithDic:result];
        [self.arrayOfRefreshData addObjectsFromArray:self.appreciate.appreciate];
       
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        [self.activityIndicatorView stopAnimating];
        
    } andFailureBlock:^(NSError *error) {}];


}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0 && self.flag == 0) {
        
        return self.appreciate.appreciate.count;

    } if(self.flag == 1){
    
        return self.arrayOfRefreshData.count;
    }
        return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (indexPath.section == 0 && self.flag == 0) {
       ViewPointView *cell = [self.tableView dequeueReusableCellWithIdentifier:@"AppreciateCell"];
        cell.title.text = self.appreciate.appreciate[indexPath.row].title;
        cell.content.text = self.appreciate.appreciate[indexPath.row].content;
        NSURL *url = [NSURL URLWithString:self.appreciate.appreciate[indexPath.row].photo];
        [cell.imageViewRight sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];
         return cell;
    }
   else if (indexPath.section == 0 && self.flag == 1) {
        
        
         Appreciate *app = self.arrayOfRefreshData[indexPath.row];
        ViewPointView *cell = [self.tableView dequeueReusableCellWithIdentifier:@"AppreciateCell"];
        cell.title.text = app.title;
        cell.content.text = app.content;
        NSURL *url = [NSURL URLWithString:app.photo];
        [cell.imageViewRight sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];
        return cell;
    }
       
         return nil;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 95;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (self.flag == 0) {
      
        
        NSString *picid = self.appreciate.appreciate[indexPath.row].uid;
        ViewPointTableviewController *point = [[ViewPointTableviewController alloc]init];
        point.viewId = picid;
        [self.navigationController pushViewController:point animated:YES];

        
    }
    
    if (self.flag == 1){
    
       Appreciate *app = self.arrayOfRefreshData[indexPath.row];
        NSString *picid = app.uid;
        ViewPointTableviewController *point = [[ViewPointTableviewController alloc]init];
        point.viewId = picid;
        [self.navigationController pushViewController:point animated:YES];
    
    }



}


@end
